package cn.joeg.app.web.core.controller;

import cn.joeg.app.web.core.service.WidgetService;
import cn.joeg.app.web.core.vo.WidgetDataReq;
import cn.joeg.app.web.core.vo.WidgetDataResp;
import com.ecfront.dew.common.JsonHelper;
import com.ecfront.dew.common.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Api(value = "Widget", description = "适配老的 url设计")
public class OldWidgetController {

    @Autowired
    private WidgetService widgetService;


    /**
     * 适配老的请求方式
     * host:port/redtower/user.jsp?param={"type":"xx"}
     *
     * @param code
     * @return
     */
    @GetMapping(value = "/{code}.jsp")
    public byte[] getDataByCode(@PathVariable String code, HttpServletRequest request) {
        String str = request.getParameter("param");
        Map map = JsonHelper.toObject(str, Map.class);
        String widgetMethod = (String) map.get("type");
        WidgetDataReq widgetDataReq = new WidgetDataReq();
        widgetDataReq.setWidgetCode(code);
        widgetDataReq.setWidgetMethod(widgetMethod);
        widgetDataReq.setArgs(JsonHelper.toJson(str));
        return widgetService.getDataByCodeForOld(code, widgetDataReq);
    }


}
